package com.dotstone.chipism.fragment;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dotstone.chipism.R;
import com.dotstone.chipism.activity.AccountActivity;
import com.dotstone.chipism.ui.login.LoginActivity;
import com.dotstone.chipism.activity.OrderActivity;
import com.dotstone.chipism.activity.RewardActivity;
import com.dotstone.chipism.activity.SettingActivity;
import com.dotstone.chipism.activity.ShareManagerActivity;
import com.dotstone.chipism.activity.TabHouseActivity;
import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.bean.HttpURL;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.NetUtil;
import com.dotstone.chipism.util.SPUtils;
import com.dotstone.chipism.util.SocketManager;
import com.dotstone.chipism.util.StatusBarHeightManager;
import com.dotstone.chipism.util.ToastShow;

public class UserFragment extends Fragment implements OnClickListener {
	public static UserFragment instance;
	private Button mQuitBtn;
	private TextView mTelT;
	public static TextView mUserNameT;
	private LinearLayout mAccountL;
	private LinearLayout mOrderL;
	private LinearLayout mHouseL;
	private LinearLayout mSystemL;
	private LinearLayout mProfitL;
	private LinearLayout mProfitL1;
	private LinearLayout mShareL;

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		instance = this;
		View view = inflater.inflate(R.layout.fragment_usercenter, container, false);
		findView(view);
		initEvent();
		return view;
	}

	private void initEvent() {
		// TODO Auto-generated method stub
		mQuitBtn.setOnClickListener(this);
		mAccountL.setOnClickListener(this);
		mOrderL.setOnClickListener(this);
		mHouseL.setOnClickListener(this);
		mSystemL.setOnClickListener(this);
		mProfitL.setOnClickListener(this);
		mProfitL1.setOnClickListener(this);
		mShareL.setOnClickListener(this);
	}

	private void findView(View view) {
		// TODO Auto-generated method stub
		mQuitBtn = (Button) view.findViewById(R.id.quit_btn);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			RelativeLayout.LayoutParams p = StatusBarHeightManager.getInstance().layoutParams2;
			if (p != null) {
				p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
				mQuitBtn.setLayoutParams(StatusBarHeightManager.getInstance().layoutParams2);
			}
		}
		mTelT = (TextView) view.findViewById(R.id.tel_tv);
		mTelT.setText((String) SPUtils.get(getActivity(), "user_name", ""));
		mUserNameT = (TextView)view.findViewById(R.id.username_tv);
		mUserNameT.setText((String)SPUtils.get(getActivity(), "nickname", ""));
		mAccountL = (LinearLayout) view.findViewById(R.id.account_safe_layout);
		mOrderL = (LinearLayout) view.findViewById(R.id.order_layout);
		mHouseL = (LinearLayout) view.findViewById(R.id.myhouse_layout);
		mSystemL = (LinearLayout) view.findViewById(R.id.system_setting_layout);
		mProfitL = (LinearLayout) view.findViewById(R.id.profit_layout);
		mProfitL1 = (LinearLayout) view.findViewById(R.id.profit_layout1);
		mShareL = (LinearLayout) view.findViewById(R.id.share_layout);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (v.getId()) {

		case R.id.quit_btn:

			DeviceManager.getInstance().clearDevice();
			DeviceManager.getInstance().clearHouse();
			DeviceManager.getInstance().clearRoom();
			DeviceManager.getInstance().clearOrder();
			List<Device> mWlans = DeviceManager.getInstance().getWlans();
			for (int i = 0; i < mWlans.size(); i++) {
				if (SocketManager.getInstance().sMap.get(mWlans.get(i).getDeviceID()) != null) {
					// SocketManager.getInstance().sMap.get(mWlans.get(i).getDeviceID())=null;
				}
			}
			signOut();
			SocketManager.getInstance().stopMqtt();
			SocketManager.getInstance().sMap.clear();
			SPUtils.clear(getActivity());
			intent = new Intent(getActivity(), LoginActivity.class);
			startActivity(intent);
			getActivity().finish();
			break;
		case R.id.account_safe_layout:
			intent = new Intent(getActivity(), AccountActivity.class);
			startActivity(intent);
		//	ToastShow.Show(getActivity(), "尚未完善");
			break;
		case R.id.order_layout:
			intent = new Intent(getActivity(), OrderActivity.class);
			startActivity(intent);
			break;
		case R.id.myhouse_layout:
			intent = new Intent(getActivity(), TabHouseActivity.class);
			startActivity(intent);
			break;
		case R.id.system_setting_layout:
			intent = new Intent(getActivity(), SettingActivity.class);
			startActivity(intent);
			break;
		case R.id.profit_layout:
			ToastShow.Show(getActivity(), "公测阶段，功能暂未开放");
			// intent = new Intent(getActivity(), RebateActivity.class);
			// startActivity(intent);
			break;
		case R.id.profit_layout1:
			// ToastShow.Show(getActivity(), "尚待完善");
			intent = new Intent(getActivity(), RewardActivity.class);
			startActivity(intent);
			break;
		case R.id.share_layout:
			intent = new Intent(getActivity(), ShareManagerActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}
	
	private void signOut(){
		if (NetUtil.isNetworkAvailable(getActivity())) {
			RequestParams params = new RequestParams(HttpURL.SIGN_OUT);
			params.setAsJsonContent(true);
			JSONObject obj = new JSONObject();
			try {
				obj.put("key", "10000");
				obj.put("token", (String)SPUtils.get(getActivity(), "token", ""));
				obj.put("requestTime", "" + System.currentTimeMillis());
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
			params.setBodyContent(obj.toString());
			x.http().post(params, new Callback.CommonCallback<String>() {
				@Override
				public void onSuccess(String result) {
					Log.e("wmy", "signout=" + result);
				}

				@Override
				public void onError(Throwable ex, boolean isOnCallback) {

				}

				@Override
				public void onCancelled(CancelledException cex) {
				}

				@Override
				public void onFinished() {
				}
			});
		} 
	}
}
